package com.bocai.yuweiwangluo.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.bean.MyCouponBean;
import com.bocai.yuweiwangluo.utils.TimeUtils;

import java.text.SimpleDateFormat;


public class CouponsListAdapter extends BaseAdapter
{
    private Context context;
    private MyCouponBean myCouponBean;
    public CouponsListAdapter(Context context,MyCouponBean myCouponBean)
    {
        this.context=context;
        this.myCouponBean=myCouponBean;
    }
    public void addData(MyCouponBean bean){
        myCouponBean.getData().addAll(bean.getData());
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount()
    {
        return myCouponBean.getData().size();
    }

    @Override
    public Object getItem(int position)
    {
        return myCouponBean.getData().get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_coupons_list,null);
            viewHolder.tvMoney=(TextView)convertView.findViewById(R.id.tv_coupons_money);
            viewHolder.tvTitle=(TextView)convertView.findViewById(R.id.tv_coupons_title);
            viewHolder.tvContent=(TextView)convertView.findViewById(R.id.tv_coupons_content);
            viewHolder.tvTime=(TextView)convertView.findViewById(R.id.tv_coupons_time);
            viewHolder.ll_coupon_bag=(LinearLayout)convertView.findViewById(R.id.ll_coupon_bag);
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MyCouponBean.Data data=myCouponBean.getData().get(position);
        viewHolder.tvMoney.setText(data.getPrice());
        viewHolder.tvTitle.setText(data.getTitle());
        SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
        String nickname=sharedPreferences.getString("nickname","xxx");
//        viewHolder.tvContent.setText("用户" + nickname + "消费优惠，单次消费仅限一张不找零");
        viewHolder.tvContent.setText(data.getIntro());
        long current=System.currentTimeMillis();
        long eTime= Long.parseLong(data.getEtime());
        if(eTime*1000-current>0){
            viewHolder.ll_coupon_bag.setBackgroundColor(context.getResources().getColor(R.color.title_bag));
            if(data.getIs_used()==0){

                viewHolder.tvTime.setText(TimeUtils.getTime(Long.parseLong(data.getEtime()),new SimpleDateFormat("yyyy年MM月dd日"))+"过期");
            }else{
                viewHolder.tvTime.setText("已使用");
            }
        }else{
            viewHolder.ll_coupon_bag.setBackgroundColor(context.getResources().getColor(R.color.couponbag));
            viewHolder.tvTime.setText("已过期");
        }
        return convertView;
    }


    public class ViewHolder
    {
        TextView tvMoney;
        TextView tvTitle;
        TextView tvContent;
        TextView tvTime;
        LinearLayout ll_coupon_bag;
    }
}
